// import requestService from '@/config/requestConfig'
import { ref } from 'vue'
import {onReachBottom} from '@dcloudio/uni-app'

export default function() {
  const listLoading = ref(false)
  const list = ref([])
  const page = ref(1)
  const rows = ref(10) // Assuming a default value, change as needed
  const localQueryUrl = ref('') // Specify the query URL

  const pageNum = ref(0);
  const count = ref(0);
  const getListSuccess = ref(null);
  const localQuery = ref({})
  const loadingStatus =ref("loading")

  const getList = (resetList: boolean, query: {}, queryUrl: string) => {

    console.log('query', query)
    localQuery.value = query
    localQueryUrl.value = queryUrl
    listLoading.value = true

    if (resetList) {
      list.value = []
      page.value = 1
    }

    const requestData = {
      page: page.value,
      rows: rows.value,
      ...query,
    }
    return uni.http({
      url: queryUrl,
      method: 'get',
      data:requestData,
      requestType:'company',
      noAuth: false
    }).then((response) => {
      setList(response)
    })
  }


  onReachBottom(async () => {
    console.log('onReachBottom')
    console.log(page.value >= pageNum.value || listLoading.value)
    if (page.value >= pageNum.value || listLoading.value) {
      return;
    }

    page.value++;
    await getList(false, localQuery.value, localQueryUrl.value);
  })

  const setList = (response: any) => {
    if (response.code === 10000) {
      const e = response.data;
      pageNum.value = e.last_page;
      count.value = e.total;
      e.data = e.data || [];

      if (page.value > 1) {
        e.data.forEach((item: any) => {
          list.value.push(item);
        });
      } else {
        list.value = e.data;
      }

      if (getListSuccess.value) {
        getListSuccess.value(e);
      }
    }

    listLoading.value = false;
  };

  return {
    getList,
    setList,
    listLoading,
    list,
    loadingStatus
  }
}
